package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entity.CommonResult;
import com.atguigu.springcloud.entity.Payment;
import com.atguigu.springcloud.exception.PaymentBusinessException;
import com.atguigu.springcloud.exception.PaymentBusinessExceptionEnum;
import com.atguigu.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * @Author lizhilong
 * @create 2020/4/19 22:04
 * @desc
 */
@RestController
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private int serverPort;

    @PostMapping("/payment/create")
    public CommonResult<Long> create(@RequestBody Payment payment) {
        log.info(">>>>>>>>>>{}<<<<<<<<<<<",serverPort);
        long i = paymentService.create(payment);
        if (i > 0) {
            return CommonResult.success(i);
        } else {
            throw new PaymentBusinessException(PaymentBusinessExceptionEnum.CREATE_ERROR);
        }
    }

    @GetMapping("/payment/get/{id}")
    public CommonResult<Payment> selectById(@PathVariable("id") Long id) {
        log.info(">>>>>>>>>>{}<<<<<<<<<<<",serverPort);
        Payment payment = paymentService.selectById(id);
        Optional.ofNullable(payment).orElseThrow(() ->  new PaymentBusinessException(PaymentBusinessExceptionEnum.RECORD_NOT_FOUND));
        return CommonResult.success(payment);
    }
}
